import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import os

# 读取数据文件
data_path = '2020010104ocean_data.csv'
df = pd.read_csv(data_path, low_memory=False)  # 避免 DtypeWarning

# 计算筛选前的数据总量
filtered_data_size = len(df)
print(f'筛选前的数据总量为：{filtered_data_size}')
df = df[df['fy_clm'] == 0]
# 筛选并分别处理不同 clt 值的数据
clt_values = [2, 3, 4, 5, 6]
processed_data = {}

def process_data(data):
    # 1. 对 fy_cth 进行 Z 得分标准化
    scaler_cth = StandardScaler()
    fy_cth_scaled = scaler_cth.fit_transform(data[['fy_cth']])
    data['fy_cth_scaled'] = fy_cth_scaled

    # 2. 对 fy_ctt 进行 Z 得分标准化
    scaler_ctt = StandardScaler()
    fy_ctt_scaled = scaler_ctt.fit_transform(data[['fy_ctt']])
    data['fy_ctt_scaled'] = fy_ctt_scaled

    # 3. 对 fy_ctp 进行 Z 得分标准化
    scaler_ctp = StandardScaler()
    fy_ctp_scaled = scaler_ctp.fit_transform(data[['fy_ctp']])
    data['fy_ctp_scaled'] = fy_ctp_scaled

    # 4. 对 fy_olr 进行 Z 得分标准化
    scaler_olr = StandardScaler()
    fy_olr_scaled = scaler_olr.fit_transform(data[['fy_olr']])
    data['fy_olr_scaled'] = fy_olr_scaled

    # 5. 处理 fy 的经纬度特征，映射为周期性特征（正弦和余弦变换）
    data['fy_lat_sin'] = np.sin(np.radians(data['fy_lat']))
    data['fy_lat_cos'] = np.cos(np.radians(data['fy_lat']))
    data['fy_lon_sin'] = np.sin(np.radians(data['fy_lon']))
    data['fy_lon_cos'] = np.cos(np.radians(data['fy_lon']))

    # 7、band1-6 进行标准化，band7-14 计算亮温并标准化

    # 亮温计算参数
    C1 = 1.19104 * 10 ** 8  # W·μm⁴·m⁻²·sr⁻¹
    C2 = 1.43877 * 10 ** 4  # μm·K

    # 确保这些列名在 DataFrame 中
    band_columns = [f'band{i}' for i in range(1, 15)]
    missing_bands = [col for col in band_columns if col not in data.columns]

    if missing_bands:
        print(f"缺少以下列: {missing_bands}")
    else:
        # 对 Band1 - Band6 直接进行标准化处理
        for band_num in range(1, 7):
            band_name = f'band{band_num}'
            scaler = StandardScaler()
            band_scaled = scaler.fit_transform(data[[band_name]])
            data[f'{band_name}_scaled'] = band_scaled

        # 对 Band7 - Band14 计算亮温后进行标准化处理
        for band_num in range(7, 15):
            band_name = f'band{band_num}'
            center_wavelength = None
            if band_num == 7:
                center_wavelength = 3.75  # 根据实际情况区分高分辨率部分的中心波长
            elif band_num == 8:
                center_wavelength = 3.75  # 根据实际情况区分低分辨率部分的中心波长
            elif band_num == 9:
                center_wavelength = 6.25
            elif band_num == 10:
                center_wavelength = 7.1
            elif band_num == 11:
                center_wavelength = 8.5
            elif band_num == 12:
                center_wavelength = 10.8
            elif band_num == 13:
                center_wavelength = 12.0
            elif band_num == 14:
                center_wavelength = 13.5

            # 计算亮温
            radiation_brightness = data[band_name]
            brightness_temperature = C2 / (center_wavelength * np.log(1 + C1 / (radiation_brightness * center_wavelength**3)))
            data[band_name + '_brightness_temperature'] = brightness_temperature

            scaler = StandardScaler()
            band_scaled = scaler.fit_transform(data[[band_name + '_brightness_temperature']])
            data[f'{band_name}_scaled'] = band_scaled

    # 计算各个通道的灰度值
    for band_num in range(1, 15):
        band_name = f'band{band_num}'
        L = data[band_name]
        Lmin = L.min()
        Lmax = L.max()
        gray_value = ((L - Lmin) * 255) / (Lmax - Lmin)
        data[f'{band_name}_gray_value'] = gray_value
    # 对灰度值进行标准化处理
    for band_num in range(1, 15):
        gray_value_col = f'band{band_num}_gray_value'
        scaler = StandardScaler()
        gray_value_scaled = scaler.fit_transform(data[[gray_value_col]])
        data[gray_value_col + '_scaled'] = gray_value_scaled

    return data
# 筛选并处理每个clt值的数据
for clt_val in clt_values:
    clt_data = df[df['fy_clt'] == clt_val]
    print(f"Processing CLT={clt_val} data...")
    processed_data[clt_val] = process_data(clt_data)

    # 保存处理后的结果到CSV文件
    output_file = f'processed_clt_{clt_val}_data.csv'
    processed_data[clt_val].to_csv(output_file, index=False)
    print(f"CLT={clt_val} data processed and saved to {output_file}.")

print("所有 CLT 数据处理完成。")
